<?php

/**
 * This is the model class for table "{{solution}}".
 *
 * The followings are the available columns in table '{{solution}}':
 * @property integer $sol_id
 * @property integer $sol_pid
 * @property string $sol_content
 * @property integer $sol_hits
 * @property integer $sol_created
 * @property integer $sol_uid
 *
 * The followings are the available model relations:
 * @property Project $project
 * @property User $user
 */
class Solution extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Solution the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{solution}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('sol_pid, sol_content', 'required'),
			array('sol_pid, sol_hits, sol_created, sol_uid', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('sol_id, sol_pid, sol_content, sol_hits, sol_created, sol_uid', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'project' => array(self::BELONGS_TO, 'Project', 'sol_pid'),
			'user' => array(self::BELONGS_TO, 'User', 'sol_uid'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'sol_id' => 'Sol',
			'sol_pid' => 'Sol Pid',
			'sol_content' => 'Sol Content',
			'sol_hits' => 'Sol Hits',
			'sol_created' => 'Sol Created',
			'sol_uid' => 'Sol Uid',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('sol_id',$this->sol_id);
		$criteria->compare('sol_pid',$this->sol_pid);
		$criteria->compare('sol_content',$this->sol_content,true);
		$criteria->compare('sol_hits',$this->sol_hits);
		$criteria->compare('sol_created',$this->sol_created);
		$criteria->compare('sol_uid',$this->sol_uid);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function getBrifeDescript($model, $length=15)
	{
		$string = $model->sol_content;
		
		$string =  strlen(strip_tags($string))?strip_tags($string):date('y-m-d',$preview->sol_created);
		
		$string = UtilTools::strSlice($string,0,$length);
		
		return $string;
	}
	
		/**
	 * This is invoked before the record is saved.
	 * @return boolean whether the record should be saved.
	 */
	protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord)
			{
				$this->sol_created=time();
				$this->sol_uid = Yii::app()->user->id;
				
				$this->sol_hits = 0;
			}

			return true;
		}
		else
			return false;
	}
	
}